package com.ruoyi.community.service.impl;

import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.community.domain.AdResource;
import com.ruoyi.community.domain.ChangeAdParams;
import com.ruoyi.community.mapper.AdResourceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.community.mapper.AdPositionCommunityMapper;
import com.ruoyi.community.domain.AdPositionCommunity;
import com.ruoyi.community.service.IAdPositionCommunityService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 广告位置Service业务层处理
 * 
 * @author hanning
 * @date 2022-04-30
 */
@Service
public class AdPositionCommunityServiceImpl implements IAdPositionCommunityService 
{
    @Autowired
    private AdPositionCommunityMapper adPositionCommunityMapper;
    @Autowired
    private AdResourceMapper adResourceMapper;

    /**
     * 查询广告位置
     * 
     * @param id 广告位置ID
     * @return 广告位置
     */
    @Override
    public AdPositionCommunity selectAdPositionCommunityById(Integer id)
    {
        return adPositionCommunityMapper.selectAdPositionCommunityById(id);
    }

    /**
     * 根据 社区id 查询广告位置列表
     * @return
     */
    @Override
    public List<AdPositionCommunity> selectListByCid() {
        return adPositionCommunityMapper.selectListByCid(SecurityUtils.getCommunityId());
    }

    /**
     * 查询广告位置列表
     * 
     * @param adPositionCommunity 广告位置
     * @return 广告位置
     */
    @Override
    public List<AdPositionCommunity> selectAdPositionCommunityList(AdPositionCommunity adPositionCommunity)
    {
        return adPositionCommunityMapper.selectAdPositionCommunityList(adPositionCommunity);
    }

    /**
     * 新增广告位置
     * 
     * @param adPositionCommunity 广告位置
     * @return 结果
     */
    @Override
    public int insertAdPositionCommunity(AdPositionCommunity adPositionCommunity)
    {
        return adPositionCommunityMapper.insertAdPositionCommunity(adPositionCommunity);
    }

    /**
     * 修改广告位置
     * 
     * @param adPositionCommunity 广告位置
     * @return 结果
     */
    @Override
    public int updateAdPositionCommunity(AdPositionCommunity adPositionCommunity)
    {
        return adPositionCommunityMapper.updateAdPositionCommunity(adPositionCommunity);
    }

    /**
     * 若已有广告投放，将原广告的状态改为 未投放 status = "0"
     * 将现广告的状态改为 投放中 status = "1"
     * 更换广告
     * @param params
     * @return
     */
    @Override
    @Transactional
    public int change(ChangeAdParams params) {
        if (params.getOriginalAdId() != null && params.getOriginalAdId() != 0) {
            AdResource originalAd = new AdResource(params.getOriginalAdId(), "0");
            adResourceMapper.updateAdResource(originalAd);
        }
        AdResource ad = new AdResource(params.getAdId(), "1");
        adResourceMapper.updateAdResource(ad);

        AdPositionCommunity adPositionCommunity = new AdPositionCommunity(params.getPosId(), params.getAdId());
        return adPositionCommunityMapper.updateAdPositionCommunity(adPositionCommunity);
    }

    /**
     * 批量删除广告位置
     * 
     * @param ids 需要删除的广告位置ID
     * @return 结果
     */
    @Override
    public int deleteAdPositionCommunityByIds(Integer[] ids)
    {
        return adPositionCommunityMapper.deleteAdPositionCommunityByIds(ids);
    }

    /**
     * 删除广告位置信息
     * 
     * @param id 广告位置ID
     * @return 结果
     */
    @Override
    public int deleteAdPositionCommunityById(Integer id)
    {
        return adPositionCommunityMapper.deleteAdPositionCommunityById(id);
    }
}
